package com.hontech.pastacooking.conn

import com.hontech.pastacooking.app.bus
import com.hontech.pastacooking.event.HeatExceptEvent
import com.hontech.pastacooking.event.HeaterStatusEvent
import com.hontech.pastacooking.event.SystemWarnEvent
import com.hontech.pastacooking.model.HeaterStatus
import com.hontech.pastacooking.net.NetClient
import com.hontech.pastacooking.net.REQ_HEAT_STATUS
import com.hontech.pastacooking.serial.ByteView
import com.hontech.pastacooking.serial.UInt16
import com.hontech.pastacooking.serial.UInt8
import com.hontech.pastacooking.utils.S

object HeaterProto {

    const val Except = 0x01
    const val OtaStart = 0x02
    const val OtaTranslate = 0x03
    const val OtaComplete = 0x04
    const val TestValve = 0x05
    const val TestPump = 0x06
    const val TestHeator = 0x07
    const val TestNozzle = 0x08
    const val TestDraw = 0x09
    const val StatusUpdate = 0x0A
    const val SetWaterParam = 0x0B
    const val SetSteamParam = 0x0C
    const val SetDrawParam = 0x0D
    const val SetFlowParam = 0x0E
    const val QueryParam = 0x0F
    const val Clean = 0x10
    const val Cooking = 0x11
    const val TestFan = 0x12
    const val DeliveryCtrl = 0x13
    const val WorkCtrl = 0x14
    const val PreProc = 0x15
    const val AutoRelease = 0x16
    const val SetMiniBucketWeight = 0x17
    const val SystemWarn = 0x18

    var status = HeaterStatus()
        private set

    private fun parseStatus(frame: Frame) {
        val tmp = HeaterStatus()
        frame.parse(
            tmp.appVersion,
            tmp.nozzle,
            tmp.waterTemp,
            tmp.steamTemp,
            tmp.steamKpa,
            tmp.sensor,
            tmp.flag,
            tmp.count,
            tmp.drawType,
            tmp.heatType
        )
        if (tmp != status) {
            NetClient.notify(REQ_HEAT_STATUS, frame.data)
        }
        status = tmp
        bus.post(HeaterStatusEvent())
    }

    private fun onRecvExcept(frame: Frame) {
        val ec = UInt8()
        val msg = ByteView()
        frame.parse(ec, msg)
        bus.post(HeatExceptEvent(ec.value, msg.toString()))
    }

    private fun onRecvSystemWarn(frame: Frame) {
        val code = UInt16()
        frame.parse(code)
        val msg = warnMsg(code.value)
        bus.post(SystemWarnEvent(msg))
    }

    fun onRecv(frame: Frame) {
        val req = frame.request()
        when (req) {
            StatusUpdate -> parseStatus(frame)
            Except -> onRecvExcept(frame)
            SystemWarn -> onRecvSystemWarn(frame)
        }
    }

    private fun warnMsg(wc: Int): String {
        return when (wc) {
            0x00 -> S.t278
            0x01 -> S.t279
            0x02 -> S.t280
            else -> S.t281
        }
    }

    fun errMsg(ec: Int): String {
        when (ec) {
            0x00 -> return S.t147
            0x01 -> return S.t148
            0x02 -> return S.t149
            0x03 -> return S.t150
            0x04 -> return S.t151
            0x05 -> return S.t152
            0x06 -> return S.t153
            0x07 -> return S.t154
            0x08 -> return S.t155
            0x09 -> return S.t156
            0x0A -> return S.t157
            0x0B -> return S.t158
            0x0C -> return S.t159
            0x0D -> return S.t160
            0x0E -> return S.t161
            0x0F -> return S.t162
            0x10 -> return S.t163
            else -> return S.t164
        }
    }

    fun drawMsg(type: Int): String {
        when (type) {
            0x00 -> return S.t165
            0x01 -> return S.t166
            0x02 -> return S.t167
            0x03 -> return S.t168
            0x04 -> return S.t169
            0x05 -> return S.t170
            0x06 -> return S.t171
            0x07 -> return S.t172
            0x08 -> return S.t173
            0x09 -> return S.t174
            0x0A -> return S.t284
            else -> return S.t175
        }
    }

    fun heatMsg(type: Int): String {
        when (type) {
            0x00 -> return S.t176
            0x01 -> return S.t177
            0x02 -> return S.t179
            0x03 -> return S.t180
            0x04 -> return S.t269
            0x05 -> return S.t270
            else -> return S.t181
        }
    }
}




















